
#include <sourcemod>
#include <sdktools>


//Define CVARS
#define MAX_SURVIVORS GetConVarInt(FindConVar("survivor_limit"))
#define MAX_INFECTED GetConVarInt(FindConVar("z_max_player_zombies"))
#define PLUGIN_VERSION "1.6"


//Handles
new Handle:cc_plpTimer = INVALID_HANDLE;
new Handle:cc_plpShowBots = INVALID_HANDLE;


//CVARS
new plpTimer;
new plpShowBots;
new maxcl;


//Plugin Info Block
public Plugin:myinfo =
{
	name = "Playerlist Panel",
	author = "OtterNas3",
	description = "Shows Panel for Teams on Server",
	version = PLUGIN_VERSION,
	url = "http://www.sourcemod.net/"
};


//Plugin start
public OnPluginStart()
{

	//Reg Commands
	RegConsoleCmd("players", PrintTeamsToClient);

	//Reg Cvars
	CreateConVar("l4d_plp_version", PLUGIN_VERSION, "Playerlist Panel Display Version", FCVAR_REPLICATED|FCVAR_PLUGIN|FCVAR_NOTIFY|FCVAR_DONTRECORD);
	cc_plpTimer = CreateConVar("l4d_plp_timer", "20", "How long, in seconds, the Playerlist Panel stay before it close automatic");
	cc_plpShowBots = CreateConVar ("l4d_plp_show_bots", "1", "Should bots be listed in Panel?");
	
	//Execute the config file
	AutoExecConfig(true, "l4d_teamspanel");

	//Hook Cvars
	HookConVarChange(cc_plpTimer, ConVarChanged);
	HookConVarChange(cc_plpShowBots, ConVarChanged);
	
	
	//Checking !REAL! MaxClients
	maxcl = maxclToolzDowntownCheck();
	
	//Re read CVARS
	ReadCvars();
}


//Search for running L4DToolz and/or L4Downtown (or none of them) to get correct Max Clients
maxclToolzDowntownCheck()
{
	new Handle:invalid = INVALID_HANDLE;
	new Handle:downtownrun = FindConVar("l4d_maxplayers");
	new Handle:toolzrun = FindConVar("sv_maxplayers");
	
	//Downtown is running!
	if (downtownrun != (invalid))
	{
		//Is Downtown used for slot patching? if yes use it for Max Players
		new downtown = (GetConVarInt(FindConVar("l4d_maxplayers")));
		if (downtown >= 1)
		{
			maxcl = (GetConVarInt(FindConVar("l4d_maxplayers")));
		}
	}

	//L4DToolz is running!
	if (toolzrun != (invalid))
	{
		//Is L4DToolz used for slot patching? if yes use it for Max Players
		new toolz = (GetConVarInt(FindConVar("sv_maxplayers")));
		if (toolz >= 1)
		{
			maxcl = (GetConVarInt(FindConVar("sv_maxplayers")));
		}
	}

	//No Downtown or L4DToolz running using fallback (possible x/32)
	if (downtownrun == (invalid) && toolzrun == (invalid))
	{
		maxcl = (MaxClients);
	}
	return maxcl;
}


//Prepare & Print Playerlist Panel
public BuildPrintPanel(client)
{
	//Get correct Max Clients
	maxcl = maxclToolzDowntownCheck();

	//Build panel
	new Handle:TeamPanel = CreatePanel();
	SetPanelTitle(TeamPanel, "## Playerlist Panel ##");
	new count;
	new i, sumall, sumspec, sumsurv, suminf;
	new String:text[64];

	//Counting
	sumall = CountAllHumanPlayers();
	sumspec = CountPlayersTeam(1);
	sumsurv = CountPlayersTeam(2);
	suminf = CountPlayersTeam(3)
	
	
	//Draw Spectators count line
	Format(text, sizeof(text), "+Spectators (%d/%d)", sumspec, sumall);
	
	DrawPanelText(TeamPanel, text);


	//Get & Draw Spectator Player Names
	count = 1;
	for (i=1;i<=MaxClients;i++)
	{
		if (IsValidPlayer(i) && GetClientTeam(i) == 1)
		{
			Format(text, sizeof(text),"  %N", i);
			DrawPanelText(TeamPanel, text);
			count++;
		}
	}
	DrawPanelText(TeamPanel, " \n");
	
	//Draw Survivors count line
	Format(text, sizeof(text), "+Survivors (%d/%d)", sumsurv, MAX_SURVIVORS);

	DrawPanelText(TeamPanel, text);


	//Get & Draw Survivor Player Names
	count = 1;
	for (i=1;i<=MaxClients;i++)
	{
		if (plpShowBots > 0)
		{
			if (IsClientConnected(i) && IsClientInGame(i) && GetClientTeam(i) == 2)
			{
				Format(text, sizeof(text), "  %N", i);
				DrawPanelText(TeamPanel, text);
				count++;
			}
		}
		else
		{
			if (IsValidPlayer(i) && GetClientTeam(i) == 2)
			{
				Format(text, sizeof(text), "  %N", i);
				DrawPanelText(TeamPanel, text);
				count++;
			}
		}
	}
	DrawPanelText(TeamPanel, " \n");
	//Draw Infected count line
	Format(text, sizeof(text), "+Infected (%d/%d)", suminf, MAX_INFECTED);

	DrawPanelText(TeamPanel, text);

	count = 1;
	for (i=1;i<=MaxClients;i++)
	{
		if (plpShowBots > 0)
		{
			if (IsClientConnected(i) && IsClientInGame(i) && GetClientTeam(i) == 3)
			{
				Format(text, sizeof(text), "%N", i);
				DrawPanelText(TeamPanel, text);
				count++;
			}
		}
		else
		{
			if (IsValidPlayer(i) && GetClientTeam(i) == 3)
			{
				Format(text, sizeof(text), "%N", i);
				DrawPanelText(TeamPanel, text);
				count++;
			}
		}
	}
	//Draw Total connected Players & Draw Final
	DrawPanelText(TeamPanel, " \n");
	Format(text, sizeof(text), "Connected: %d/%d", sumall, maxcl);
	DrawPanelText(TeamPanel, text);

	SendPanelToClient(TeamPanel, client, TeamPanelHandler, plpTimer);
	CloseHandle(TeamPanel);

}


//TeamPanelHandler
public TeamPanelHandler(Handle:TeamPanel, MenuAction:action, param1, param2)
{
	
}




//Send the Panel to the Client
public Action:PrintTeamsToClient(client, args)
{
	
	plpTimer = GetConVarInt(cc_plpTimer);
	BuildPrintPanel(client);
}


//Cvar changed check
public ConVarChanged(Handle:convar, const String:oldValue[], const String:newValue[])
{
	ReadCvars();
}


//Re-Read Cvars
public ReadCvars()
{
	plpTimer=GetConVarInt(cc_plpTimer);
	plpShowBots=GetConVarInt(cc_plpShowBots);

}

//Is Valid Player
public IsValidPlayer(client)
{
	if (client == 0)
		return false;
	
	if (!IsClientConnected(client))
		return false;
	
	if (IsFakeClient(client))
		return false;
	
	if (!IsClientInGame(client))
		return false;
	
	return true;
}


//Count all Players
public CountAllHumanPlayers()
{
	new Count = 0;
	for (new i=1;i<=MaxClients;i++)
	{
		if (IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i))
		{
			Count++;
		}
	}
	return Count;
}


//Count Players Team
public CountPlayersTeam(team)
{
	new Count = 0;
	for (new i=1;i<=MaxClients;i++)
	{
		if (IsClientConnected(i) && IsClientInGame(i) && !IsFakeClient(i) && GetClientTeam(i) == team)
		{
			Count++;
		}

	}
	return Count;
}


//End of Plugin
